var primePools = [];
var primes = [];

function getPrimes(limit) {
    var sqrt = Math.floor(Math.sqrt(limit));
    primePools[1] = 1;
    primePools[2] = 0;
    for(var i=2; i<=sqrt; i++) {
        for(var j=i*2; j<=limit; j=j+i) {
            primePools[j] = 1;
        }
    }
}

function isPrime(num) {
    return primePools[num] != 1
}

print('get primes.....');
var t=new Date().getTime();
var plimit = 1000000
getPrimes(plimit);
for(var i=2; i<=plimit; i++) {
    if(isPrime(i)) primes.push(i);
}
print('get primes done..... ' + ((new Date().getTime()-t)/1000) + "s");

var count=0, sum=0;
for(var i=1; i<primes.length; i++) {
    if(primes[i]<10) continue;
    pass = false;
    var tmp = primes[i];
    do {
        tmp = (tmp-tmp%10)/10;
        if(!isPrime(tmp)) {
            pass = true;
        }
    } while(tmp>0);
    tmp = primes[i];
    var base=10, tmp1=0;
    do {
        tmp1 = tmp%base;
        //print("tmp=" + tmp + ",base=" +base);
        if(!isPrime(tmp1)) {
            pass = true;
        }
        base=base*10;
    } while(tmp1!=tmp && pass !== true);
    if(pass) continue;
    print(primes[i]);
    sum += primes[i];
}
print("sum=" sum);







